﻿package {
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import org.flixel.*;
	
	public class Player extends Ship {
		private var _score:int;
		private var _tries:int;
		private var _invecible:Boolean;
		private var _invencible_time:Timer;

		public function Player(x:int, y:int) {
			super(x, y);
			loadGraphic(ResourceManager.hero, true);

			addAnimation("flying", [0, 1, 2], 20, true);
			addAnimation("invencible", [3,1,2], 20, true);
			play("flying");
			
			width = 40;
			height = 44;
			offset.y = 15;
			offset.x = 10;

			_default_velocity = 150;
            _max_bullets = 20;
			health = 10;
    
            var s:FlxSprite;
            for(var i:int = 0;i <_max_bullets;i++) {
                var shot:ShotSingle = new ShotSingle(this);
                _bullets.add(shot);
            }
			
			_invencible_time = new Timer(5000, 0);
			_invencible_time.addEventListener(TimerEvent.TIMER, tempInvencible);	
			setInvecible(true);

            FlxG.log("Player:constructor");
		}

		private function tempInvencible(event:TimerEvent):void
		{
			setInvecible(false);
		}
		
		override public function update():void {
			testKeys();
			super.update();	
			testBorderCollsions();
		}
		
		override public function kill():void {
			FlxG.log("Player:kill");
			FlxG.play(ResourceManager.player_dies);
			
			var ex:Explosion = new ExplosionMajor();
			FlxG.state.add(ex);
			ex.explode(x, y + 17);
		
			super.kill();
		}
		
		public function updateScore(score:int):void {
			_score += score;
		}
		
		public function updateTries(tries:int):void {
			_tries += tries;
		}
		
		public function getScore():int {
			return _score;
		}
		
		public function getTries():int {
			return _tries;
		}
		
		public function setInvecible(v:Boolean):void {
			_invecible = v;
			
			if (v) {
				play("invencible");
				_invencible_time.start();
			}else {
				play("flying");
			}
		}
		
		public function isInvencible():Boolean {
			return _invecible;
		}
		
		private function testBorderCollsions():void {
			/* Limits begin */
			if(x > FlxG.width-width - 9)
				x = FlxG.width-width - 9;
			if(x < 10)
				x = 10;

			if((y + height) > FlxG.height)
				y = FlxG.height - height;
				
			if(y < 0)
				y = 0;
		}
	
        override protected function shot():void {
            var b:FlxSprite = _bullets.members[_bullets_index];
            b.reset((x + (width/2)-5), y);
            FlxG.state.add(b);
            b.velocity.y = -140;
            StatePlay(FlxG.state)._playerShot.add(b);
            _bullets_index++;
            if(_bullets_index >= _bullets.members.length)
                _bullets_index = 0;
        }

		private function testKeys():void {
			velocity.x = 0;
			if(FlxG.keys.LEFT)
				velocity.x -= _default_velocity;
			if(FlxG.keys.RIGHT)
				velocity.x += _default_velocity;			

			velocity.y = 0;
			if(FlxG.keys.UP)
				velocity.y -= _default_velocity;
			if(FlxG.keys.DOWN)	
				velocity.y += _default_velocity;
	
			if(FlxG.keys.justPressed("X")) {
                shot();
			}
		
            if (FlxG.debug) {
                if (FlxG.keys.justPressed("C")) {
                    setInvecible(true);
                }
                
                if (FlxG.keys.justPressed("V")) {
                    setInvecible(false);
                }			
                
                if (FlxG.keys.justPressed("K")) {
                    kill();
                }
                
                if(FlxG.keys.justPressed("Q")) {
                    upDefaultVelocity(20)
                }
            }
		}
	}
}
